package com.atguigu.common.validator.group;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * 自定义校验器
 */
public class ListValueConstractValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set = new HashSet<>();

    /**
     * @param constraintAnnotation:是 字段注解@ListValue(vals = {0,1}) 的 0,1
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();// 0,1
        for (int a :vals){
            set.add(a);
        }
    }

    /**
     * @param integer:  用户实际输入的 showStatus
     * @param constraintValidatorContext
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);// 看有没有包含0,1
    }
}
